package com.huiyeji.hui.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.huiyeji.hui.entity.User;
import com.huiyeji.hui.entity.UserInfoEntity;
import com.huiyeji.hui.exception.HuiException;
import com.huiyeji.hui.mapper.UserMapper;
import com.huiyeji.hui.properties.UserDefaultProperties;
import com.huiyeji.hui.service.UserService;
import com.huiyeji.hui.utils.ErrorEnum;
import com.huiyeji.hui.utils.PageUtil;
import com.huiyeji.hui.vo.PageResult;
import com.huiyeji.hui.vo.SelfAndOthersAndGroupsVo;
import com.huiyeji.hui.vo.SimpleOtherVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

/**
 * @author zhaoduo
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper mapper;

    @Autowired
    private UserDefaultProperties userDefaultProperties;

    @Override
    public User getUserInfo(UserInfoEntity userInfo) {
        User user = mapper.getUserInfo(userInfo.getUsername());
        if (user != null){
            boolean checkOk = DigestUtil.bcryptCheck(userInfo.getPassword(), user.getPassword());
            if (checkOk){
                return user;
            }
        }
        return null;
    }

    @Override
    public void addUser(UserInfoEntity userInfo) {
        User user = new User();
        BeanUtils.copyProperties(userDefaultProperties,user);
        BeanUtils.copyProperties(userInfo,user);
        user.setCreateTime(LocalDateTime.now());
        int row = mapper.insertSelective(user);
        if (row == 0){
            throw new HuiException(ErrorEnum.REGISTER_ERROR);
        }
    }

    @Override
    public boolean baseModify(User requestUser) {
        return mapper.baseModify(requestUser) == 1;
    }

    @Override
    public void changePwd(Long uid, String pwd) {
        int row = mapper.updatePwd(uid,pwd);
        if (row == 0){
            throw new HuiException(ErrorEnum.MODIFY_ERROR);
        }
    }

    @Override
    public List<SelfAndOthersAndGroupsVo.Other> getOthers(Long uid, Set<Long> ids) {
        return mapper.getOthers(uid,ids);
    }

    @Override
    public void changeHeadshot(User user, String headshot) {
        int row = mapper.changeHeadshot(user.getUid(),headshot);
        if (row != 1){
            throw new HuiException(ErrorEnum.MODIFY_ERROR);
        }
        user.setHeadshot(headshot);
    }

    @Override
    public PageResult<SimpleOtherVo> keySearch(Long uid, PageUtil.KeyPageParam pp) {
        List<SimpleOtherVo> record = mapper.keySearch(uid,pp);
        return PageResult.build(1,1,record);
    }

    @Override
    public PageResult<SimpleOtherVo> conditionSearch(Long uid, PageUtil.UserConditionPageParam pp) {
        int size = mapper.conditionSearchSize(uid,pp);
        List<SimpleOtherVo> record = mapper.conditionSearch(uid,pp);
        return PageResult.build(pp.getPageNum(),size,record);
    }

    @Override
    public SimpleOtherVo getSimpleOther(Long uid) {
        return mapper.getSimpleOther(uid);
    }

    @Override
    public SelfAndOthersAndGroupsVo.Other getOther(Long uid, Long oid) {
        return mapper.getOther(uid,oid);
    }
}
